<?php

/**
 * midia actions.
 *
 * @package    sispatr
 * @subpackage midia
 * @author     Creatix
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class midiaActions extends sfActionsCreatix
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('midia', 'list');
  }
  
    public function executeList()
	{
        $paginaAtual = $this->_setActualPage();
        $this->filter = $this->_getFilter();
        $listQuery = MidiaTable::consultarMidia($this->filter);
        
        ## cria o paginador
        $this->getContext()->getConfiguration()->loadHelpers('Url');
		$this->pager_layout = new Creatix_Pager_Layout(
			$pager = new Doctrine_Pager(
				$listQuery,
				$paginaAtual,
				NUM_LIST_ITENS
			),
			new Doctrine_Pager_Range_Sliding(array('chunk' => MAX_PAGER_LINKS)),
			url_for('midia/list')
		);
		
		## executa a consulta
		$this->midia_list = $this->pager_layout->execute(array(), Doctrine::HYDRATE_ARRAY);
		
        $this->setTemplate('index');
    }
    
    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE)
		); 

		$this->getUser()->setAttribute(
			sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);
		
		return $this->redirect($this->getModuleName().'/list');
	}

  public function executeNew(sfWebRequest $request)
  {
  		$this->ref_name     = $this->getRequestParameter("ref");
        $this->ref_value    = $this->getRequestParameter("ref_value");
        $this->title		= "Cadastro de arquivo";
    	$this->form 		= new MidiaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));

    $this->form = new MidiaForm();

    $this->processForm($request, $this->form);
	return sfView::NONE;
    //$this->setTemplate('new');
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($midia = Doctrine::getTable('Midia')->find(array($request->getParameter('id_midia'))), sprintf('Object midia does not exist (%s).', $request->getParameter('id_midia')));
    $this->form = new MidiaForm($midia);

    $this->processForm($request, $this->form);
	exit();
    return sfView::NONE;
    //$this->setTemplate('edit');
  }

  public function executeArquivos()
    {
        $this->ref_name     = $this->getRequestParameter("ref_name");
        $this->ref_value    = $this->getRequestParameter("ref_value");
        $this->hasmany      = $this->getRequestParameter("hasmany");

        $id_name    = $this->ref_name;

        $this->reference    = $id_name;

        $this->midia  = MidiaTable::carregarMidia(array($id_name=>$this->ref_value));

    }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($midia = Doctrine::getTable('Midia')->find(array($request->getParameter('id_midia'))), sprintf('Object midia does not exist (%s).', $request->getParameter('id_midia')));
    
    $this->title		= "Edição de arquivo";
    $this->form 		= new MidiaForm($midia);
    $this->setTemplate('new');
  }
  


  public function executePreview()
  {
    $this->midia = Doctrine::getTable('Midia')->find(array($this->getRequestParameter('id_midia')));
  }

  public function executeDelete(sfWebRequest $request)
  {
    try
    {
    $request->checkCSRFProtection();

    $this->forward404Unless($midia = Doctrine::getTable('Midia')->find(array($request->getParameter('id_midia'))), sprintf('Object midia does not exist (%s).', $request->getParameter('id_midia')));
    $midia->delete();

    $this->redirect('midia/index');
    }
    catch(Excepiton $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
    	$this->redirect('midia/index');
	}

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
    	$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  		
    	if($this->getRequestParameter('ref_name'))
  		{
	  		$data	= explode("_", $this->getRequestParameter('ref_name'));
	  		$id		= "set".ucfirst($data[0]).ucfirst($data[1]);
	  		$form->getObject()->$id($this->getRequestParameter("ref_value"));
  		}

	    if ($form->isValid())
	    {
	      $midia = $form->save();
	      echo "Operação efetuada com sucesso";
//		  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
//	      $this->redirect('midia/edit?id_midia='.$midia->getIdMidia());
	    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
